package com.sky.service.impl;

import com.sky.context.UserHolder;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public List<AddressBook> findList(AddressBook addressBook) {
        return addressBookMapper.findList(addressBook);
    }

    // 新增地址
    @Override
    public void save(AddressBook addressBook) {
        //1. 补齐二个参数
        addressBook.setUserId(UserHolder.get());// 登录人ID
        addressBook.setIsDefault(0);// 非默认地址
        //2. 调用mapper保存
        addressBookMapper.save(addressBook);
    }

    // 设置默认地址
    @Override
    public void setDefault(AddressBook addressBook) {
        //1. 设置当前用户所有收货地址为非默认
        Long userId = UserHolder.get();
        addressBookMapper.updateByUserId(userId);
        //2. 根据当前地址id，设置为默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }

    //回显地址
    @Override
    public AddressBook findById(Long id) {
        return addressBookMapper.findById(id);
    }

    //修改地址
    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    //删除地址
    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }
}
